package org.gangcai.mac.shop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;

import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.bean.CommonResonseBean;
import org.gangcai.mac.shop.common.MultiStatusActivity;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.manager.RetrofitManager;
import org.gangcai.mac.shop.oberver.CommonObserver;
import org.gangcai.mac.shop.utils.AddressUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class AddAddressActivity extends MultiStatusActivity implements AddressUtils.CallBack {

    @BindView(R.id.username)
    EditText username;
    @BindView(R.id.identifyNumber)
    EditText identifyNumber;
    @BindView(R.id.shixian)
    TextView shixian;
    @BindView(R.id.address)
    EditText address;
    @BindView(R.id.save)
    Button save;
    private String shiqu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitleBarView(titlebar, "添加收货地址");
        ButterKnife.bind(this);


    }

    @Override
    public int getContentView() {
        return R.layout.activity_add_address;
    }

    @OnClick({R.id.shixian, R.id.save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.shixian:
                AddressUtils addressUtils = new AddressUtils(this);
                addressUtils.show(this);
                break;
            case R.id.save:
                submitData();
                break;
        }
    }

    private void submitData() {

        RetrofitManager.create().add_address(SPUtils.getInstance().getString("uid"),
                username.getText().toString(),
                identifyNumber.getText().toString(),
                shixian.getText().toString(), address.getText().toString()
        ).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<CommonResonseBean>(prompDialog) {
                    @Override
                    public void onSuccess(CommonResonseBean value) {
                        if (value.getCode() == Constant.CODE_SUCC) {
                            ToastUtils.showShort("添加地址成功");
                            setResult(RESULT_OK);
                            finish();
                        }
                    }
                });

    }

    @Override
    public void callBack(String sheng, String shi, String qu) {
        shiqu = sheng + shi + qu;
        shixian.setText(shiqu);
    }

    public static void openForResult(int requestCode) {
        ActivityUtils.
                getTopActivity().
                startActivityForResult(new Intent(ActivityUtils.getTopActivity(), AddAddressActivity.class), requestCode);
    }
}
